const tree = {
  name: '1',
  type: 'root',
  child: [
    {
      name: 'child1',
      type: 'parent',
      child: [
        {
          type: 'son',
          name: 'child1_chile1',
        },
      ],
    },
    {
      name: 'child2',
      type: 'parent',
      child: [
        {
          name: 'child2_chile1',
          type: 'son',
        },
      ],
    },
  ],
}
const foo = (tree) => {
  const ctx = []
  const traverse = (item, ctx) => {
    // 进入节点
    ctx.push(item.name)
    if (item.child) {
      item.child.forEach((element) => {
        traverse(element, ctx)
      })
    }
    // 离开节点
  }
  traverse(tree, ctx)
  return ctx
}
console.log(foo(tree))
